package com.operate.api.operate.user.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import com.github.pagehelper.PageInfo;
import com.operate.api.base.*;
import com.operate.api.operate.user.domain.vo.*;
import com.operate.api.operate.user.service.IOperateUserService;
import com.operate.util.BindingResultUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@Api( tags = ApiIndex.OperateUserController)
@RequestMapping(value = "/api/operate/user")
@RestController
public class OperateUserController {


    @Autowired
    private IOperateUserService service;


    @ApiOperation(value = "用户登录", notes = "登录后取到接口得到token(测试账户/密码【test/E10ADC3949BA59ABBE56E057F20F883E】)")
    @PostMapping(value = "/login")
    @ResponseBody
    public R<UserLoginResVO> login(@ApiParam(value = "查询参数") @Valid @RequestBody UserLoginReqVO userLogin, BindingResult bindingResult) {
        R result= BindingResultUtil.BindingResult(bindingResult);
        if(result.getCode()== Constants.SYS_SUCCESS){
            UserLoginResVO userLoginResVO = service.login(userLogin);
            result.setData(userLoginResVO);
        }
        return result;
    }


    @ApiOperation(value = "账号退出登录")
    @PostMapping(value = "/login_out")
    public R login_out() {
        service.loginOut();
        return new R();
    }


    @ApiOperation(value = "查询用户列表")
    @GetMapping(value = "/list")
    @SaCheckLogin
    public R<RestPage<List<UserVO>>> list(
            @ApiParam(value = "查询参数") @ModelAttribute UserSearchVO searchVO) {
        List<UserVO> list = service.getList(searchVO);
        RestPage restPage = new RestPage( new PageInfo(list),list);
        return new R(restPage);
    }


    @ApiOperation(value = "查询当前登录人信息")
    @GetMapping(value = "/one")
    @SaCheckLogin
    public R<UserVO> list() {
        UserVO userVo = service.getOne();
        return new R(userVo);
    }

    @ApiOperation(value = "创建用户")
    @PostMapping(value = "/add")
    @SaCheckLogin
    public R create(@ApiParam(value = "添加参数") @Valid @RequestBody UserAddVO vo, BindingResult bindingResult) {
        R result= BindingResultUtil.BindingResult(bindingResult);
        if(result.getCode()== Constants.SYS_SUCCESS){
            service.add(vo);
        }
        return result;
    }

    @ApiOperation(value = "修改当前登录用户密码")
    @PostMapping(value = "/update_password")
    @SaCheckLogin
    public R update_password(@ApiParam(value = "参数") @Valid @RequestBody UserPasswordUpdateVO passwordUpdateVO, BindingResult bindingResult) {
        R result= BindingResultUtil.BindingResult(bindingResult);
        if(result.getCode()== Constants.SYS_SUCCESS){
            service.updatePassword(passwordUpdateVO);
        }
        return result;
    }

    @ApiOperation(value = "删除平台用户")
    @PostMapping(value = "/delete")
    @SaCheckLogin
    public R delete( @ApiParam(value = "修改参数") @Valid @RequestBody IDEntity delete) {
        service.delete(delete.getId());
        return new R();
    }

    @ApiOperation(value = "禁言/启用-用户")
    @PostMapping(value = "/disableUser")
    @SaCheckLogin
    public R disableUser(@ApiParam(value = "参数")  @RequestBody UserStateVO vo) {
        service.disableUser(vo);
        return new R();
    }

    @ApiOperation(value = "修改用户")
    @PostMapping(value = "/update")
    @SaCheckLogin
    public R update(@ApiParam(value = "修改参数") @Valid  @RequestBody UserUpdateVO updateVO, BindingResult bindingResult) {
        R result= BindingResultUtil.BindingResult(bindingResult);
        if(result.getCode()== Constants.SYS_SUCCESS){
            service.update(updateVO);
        }
        return result;
    }


    @ApiOperation(value = "重置用户密码")
    @PostMapping(value = "/resetPassword")
    @SaCheckLogin
    public R resetPassword(@ApiParam(value = "用户ID") @Valid @RequestBody UserPasswordVO updateVO) {
        service.resetPassword(updateVO);
        return new R();
    }


}
